package com.turing.selfwebservice.merchantoper;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.turing.post.webservice.client.CommonWebResult;
import com.turing.post.webservice.client.MchTradDetail;
import com.turing.post.webservice.client.Merchant;
import com.turing.post.webservice.client.MerchantOper;
import com.turing.selfwebservice.common.BaseServlet;
import com.turing.selfwebservice.common.Config;
import com.turing.selfwebservice.common.OptimalPageControl;
import com.turing.selfwebservice.util.ServletUtil;

/**
 * 商户操作员Servlet
 * 
 * @author 李治
 * 
 */
public class MerchantOperServlet extends BaseServlet {

	public ActionForward MerchantOperInfo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String _url = "merchantOperInfo";
		try {
			if (webWscImplPortType == null)
				returnError(mapping, request);
			MerchantOper merchantOperInfo = webWscImplPortType
					.getMerchantOperByMrchOperId(merchantOperId);
			request.setAttribute("MerchantOper", merchantOperInfo);
			Merchant merchant = webWscImplPortType
					.getMerchantByMerchantNo(merchantOper.getMerchantNo());
			request.setAttribute("merchantName", merchant.getMerchantName());
		} catch (Exception e) {
			e.printStackTrace();
			returnError(mapping, request);
			_url = "error";
		}
		return mapping.findForward(_url);
	}

	public ActionForward UpdateMerchantOperInfo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			if (webWscImplPortType == null)
				returnError(mapping, request);
			String mrchOperName = getParameter(request, "mrchOperName");
			String sex = getParameter(request, "sex");
			String linkTele = getParameter(request, "linkTele");
			String email = getParameter(request, "email");
			String question1 = getParameter(request, "question1");
			String answer1 = getParameter(request, "answer1");
			String question2 = getParameter(request, "question2");
			String answer2 = getParameter(request, "answer2");
			String question3 = getParameter(request, "question3");
			String answer3 = getParameter(request, "answer3");
			MerchantOper oper = new MerchantOper();
			oper.setMrchOperId(merchantOperId);
			oper.setMrchOperName(mrchOperName);
			oper.setSex(sex);
			oper.setLinkTele(linkTele);
			oper.setEmail(email);
			oper.setQuestion1(question1);
			oper.setQuestion2(question2);
			oper.setQuestion3(question3);
			oper.setAnswer1(answer1);
			oper.setAnswer2(answer2);
			oper.setAnswer3(answer3);
			CommonWebResult commonWebResult = webWscImplPortType
					.updateMerchantOper(oper);
			if (commonWebResult.isSuccess()) {
				request.setAttribute("UpdateMsg", "更新商户操作员信息成功!");
			} else {
				request.setAttribute("UpdateMsg", "更新商户操作员信息失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnError(mapping, request);
		}
		ActionForward af = new ActionForward();
		af.setPath("/merchantOperServlet.do?method=MerchantOperInfo");
		return af;
	}

	// 修改密码

	public ActionForward UpdateMerchantUserPwd(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			if (webWscImplPortType == null)
				returnError(mapping, request);
			String meroldpassword = request.getParameter("meroldpassword");
			String mernewpassword = getParameter(request, "mernewpassword");
			MerchantOper merchantoper = new MerchantOper();
			merchantoper.setMrchOperId(merchantOperId);
			merchantoper.setMrchOperLogonName(merchantOperUserName);
			merchantoper.setMrchOperPasswd(meroldpassword);
			CommonWebResult resultString = webWscImplPortType
					.changeMerchantOperPwd(merchantoper, mernewpassword);
			if (resultString.isSuccess()) {
				request.setAttribute("UpdateMsg", "修改密码成功！");
			} else {
				request.setAttribute("UpdateMsg", resultString
						.getExcepMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnError(mapping, request);
		}
		ActionForward af = new ActionForward();
		af.setPath("/merchantOperServlet.do?method=MerchantOperInfo");
		return af;
	}

	/**
	 * 商户交易明细
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward MerchantTradDetails(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String _url = "merchantTradDetail";
		try {
			if (webWscImplPortType == null)
				returnError(mapping, request);
			String terminalNo = getParameter(request, "terminalNo");// 端口号
			String startTime = getParameter(request, "startTime");// 起始交易时间
			String endTime = getParameter(request, "endTime");// 终止交易时间
			String startAmountStr = getParameter(request, "startAmount");// 其实交易金额
			BigInteger startAmount = null;
			if (!ServletUtil.isEmpty(startAmountStr))
				startAmount = new BigInteger(startAmountStr);
			String endAmountStr = getParameter(request, "endAmount");// 终止交易金额
			BigInteger endAmount = null;
			if (!ServletUtil.isEmpty(endAmountStr))
				endAmount = new BigInteger(endAmountStr);
			if (!ServletUtil.isEmpty(startTime)
					&& !ServletUtil.isEmpty(endTime)) {
				Integer total = webWscImplPortType
						.getMchTradDetailTotalByCondition(merchantNo.substring(
								0, 6), merchantNo, null, terminalNo, startTime,
								endTime, startAmount, endAmount, null, null);
				int pgNo = getIntParameter(request, "pageNo", 1);
				List<MchTradDetail> mchTradDetails = webWscImplPortType
						.getMchTradDetailListByCondition(merchantNo.substring(
								0, 6), merchantNo, null, terminalNo, startTime,
								endTime, startAmount, endAmount, null, null,
								pgNo - 1);
				request.setAttribute("mchTradDetails", mchTradDetails);
				int pgCount = Integer.parseInt(String.valueOf(total));
				// 封装分页查询对象
				OptimalPageControl opg = new OptimalPageControl(
						Config.PAGE_SIZE, pgNo, pgCount);
				// 组装分页组件的请求URL
				String pageUrl = "merchantOperServlet.do?method=MerchantTradDetails"
						+ merchantNo;
				if (!ServletUtil.isEmpty(terminalNo))
					pageUrl += "&terminalNo=" + terminalNo;
				if (!ServletUtil.isEmpty(startTime))
					pageUrl += "&startTime=" + startTime;
				if (!ServletUtil.isEmpty(endTime))
					pageUrl += "&endTime=" + endTime;
				request.setAttribute("pageInfo", opg);// 翻页参数
				request.setAttribute("servletPath", pageUrl);// 翻页路径
				_url = "merchantTradDetail";
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnError(mapping, request);
			_url = "error";
		}
		return mapping.findForward(_url);
	}
}
